import os
import sys


def read_layout_problem(file_path):
    # Your p1 code here
    problem = {
        'seed': 0,
        'map': [],
        'pacmanLoc': [],
        'ghostLoc': {},
        'foodLoc': {},
        'ghostList': [],
    }

    with open(file_path) as f:
        lines = f.readlines()
        problem['seed'] = int(lines[0].split(" ")[1])
        i = 0
        for line in lines[1:]:
            tempBuffer = []
            line = line.rstrip()
            for j, ch in enumerate(line):
                if ch == '%':
                    tempBuffer.append(ch)
                else:
                    tempBuffer.append(' ')

                if ch == 'P':
                    problem['pacmanLoc'].append(i)
                    problem['pacmanLoc'].append(j)
                elif ch == 'X' or ch == 'W' or ch == 'Y' or ch == 'Z':
                    problem['ghostLoc'][ch] = [i, j]
                    problem['ghostList'].append(ch)
                elif ch == '.':
                    problem['foodLoc'][(i, j)] = ''

            problem['map'].append(tempBuffer)
            i += 1

    problem['ghostList'].sort()

    return problem


if __name__ == "__main__":
    if len(sys.argv) == 3:
        problem_id, test_case_id = sys.argv[1], sys.argv[2]
        problem = read_layout_problem(os.path.join(
            'test_cases', 'p'+problem_id, test_case_id+'.prob'))
        print(problem)
    else:
        print('Error: I need exactly 2 arguments!')
